
public class MyArrayList implements IArrayList{
    private int[] array;
    private int used_size;

    private int capacity = 10;

    public MyArrayList(){
        array = new int[capacity];
    }
    public MyArrayList(int capacity){
        array = new int[capacity];
        this.capacity = capacity;
    }
    private void grow() {
        int new_capacity = capacity * 2;
        int[] tmp = new int[new_capacity];
        System.arraycopy(array, 0, tmp, 0, used_size);
        array = tmp;
        capacity = new_capacity;
    }
    private void checkPos(int pos) throws PosIllegal{
        if(pos < 0 || pos > used_size){
            throw new PosIllegal("位置不合法：" + pos);
        }
    }

    @Override
    public int get(int pos) {

        return 0;
    }

    @Override
    public void add(int data){
        // 尾插
        add(used_size, data);
    }

    @Override
    public boolean contains(int toFind) {
        return indexOf(toFind) >= 0;
    }

    @Override
    public int indexOf(int toFind) {
        for(int i = 0; i < used_size; ++i){
            if(array[i] == toFind) return i;
        }
        return -1;
    }

    @Override
    public void add(int pos, int data) {
        try{
            checkPos(pos);
            if(isfull()) grow();

            for(int i = used_size - 1; i >= pos; i--){
                array[i + 1] = array[i];
            }
            array[pos] = data;
            used_size++;    //插入成功后才++
        }catch (PosIllegal e) {
            System.out.println("Pos非法");
            e.printStackTrace();
        }
    }

    @Override
    public boolean isfull() {
        return used_size > capacity - 1;
//        return used_size >= capacity;
    }

    @Override
    public void show() {
        int cnt = 0;
        for(int i = 0; i < used_size; ++i){
            System.out.print(array[i] + " ");
            cnt++;
        }
        System.out.println("\ntotal item: " + cnt);
    }


}
